#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

const int MAXN = 1e5 + 5;
const int MAXLOG = 20;  // 2^20 > 1e5
vector<int> tree[MAXN];
int depth[MAXN];
int parent[MAXN][MAXLOG];

void dfs(int node, int prev) {
    for(int i = 1; i < MAXLOG; i++) {
        parent[node][i] = parent[parent[node][i - 1]][i - 1];
    }
    for(int child : tree[node]) {
        if(child != prev) {
            depth[child] = depth[node] + 1;
            parent[child][0] = node;
            dfs(child, node);
        }
    }
}

int lca(int u, int v) {
    if(depth[u] < depth[v]) swap(u, v);
    for(int i = MAXLOG - 1; i >= 0; i--) {
        if(depth[u] - (1 << i) >= depth[v]) {
            u = parent[u][i];
        }
    }
    if(u == v) return u;
    for(int i = MAXLOG - 1; i >= 0; i--) {
        if(parent[u][i] != parent[v][i]) {
            u = parent[u][i];
            v = parent[v][i];
        }
    }
    return parent[u][0];
}

int main() {
    int N, Q;
    cin >> N;
    for(int i = 1; i < N; i++) {
        int u, v;
        cin >> u >> v;
        tree[u].push_back(v);
        tree[v].push_back(u);
    }
    dfs(1, -1);
    cin >> Q;
    while(Q--) {
        int a, b;
        cin >> a >> b;
        cout << lca(a, b) << endl;
    }
    return 0;
}
